﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using VietPublisher.API.Membership;
using VietPublisher.Membership;
using VietPublisher.BSL.BK;
using VietPublisher.BaseProject.Models.cart;
using VietPublisher.BaseProject.Utils.Miscellaneous;
using VietPublisher.BaseProject.Models;

namespace VietPublisher.Repository.Repositories.Concrete
{
    public class BaseRepository
    {
        #region properties

        private VIETPUBLISHEREntities _BookStoreContext;
        public VIETPUBLISHEREntities ContextBookStore
        {
            get
            {
                if (_BookStoreContext == null)
                {
                    _BookStoreContext = new VIETPUBLISHEREntities();
                }
                return _BookStoreContext;
            }
            set {
                _BookStoreContext = value;
            }
        }
        
        private MembershipEntities _MembershipContext;
        public MembershipEntities ContextMembership
        {
            get
            {
                if (_MembershipContext == null)
                {
                    _MembershipContext = new MembershipEntities();
                }
                return _MembershipContext;
            }
            set
            {
                _MembershipContext = value;
            }
        }

        IMembershipService _Membership;
        public IMembershipService Membership
        {
            get
            {
                if (_Membership == null)
                {

                    _Membership = new MembershipAPI();
                }
                return _Membership;
            }
        }


        #region Cart 
        public string UserName
        {
            get
            {
                return HttpContext.Current.Session[DConst.userName] == null ? string.Empty : HttpContext.Current.Session[DConst.userName].ToString();
            }
        }

        #region Property Cart
        public Cart ShoppingCart
        {
            get
            {
                Cart cartTempt;
                if (this.UserName != string.Empty)
                {                 
                    cartTempt = Profile.GetProfile(this.UserName).cart;
                    if (cartTempt.items.Count == 0)
                    {
                        cartTempt = (Cart)HttpContext.Current.Session[DConst.sessionCart];
                    }
                    else {
                       HttpContext.Current.Session[DConst.sessionCart] = cartTempt;
                    }

                }
                else
                {
                    cartTempt = (Cart)HttpContext.Current.Session[DConst.sessionCart];
                }
                if (cartTempt == null) {
                    cartTempt = new Cart();
                }
                return  cartTempt;
            }
            set
            {
                HttpContext.Current.Session[DConst.sessionCart] = value;
                if (this.UserName != string.Empty)
                {
                    Profile.SaveProfile(value);                    
                }
            }
        }
        #endregion
        #endregion
        #endregion


    
    }
}
